<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
	xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body>
	<div class="layui-fluid">
		<table id="role-table" lay-filter="role-table"></table>
	</div>
	<!-- 工具栏 -->
	<script type="text/html" id="toolbarRole">
        <div class="layui-btn-container">
			<button shiro:hasPermission="sys:role:add" class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle" ></i>添加</button>
            <button shiro:hasPermission="sys:role:batchRemove" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchRemove"><i class="layui-icon layui-icon-delete"></i>删除</button>
        </div>
    </script>
	<!-- 操作列 -->
	<script type="text/html" id="role-state">
     <div class="layui-btn-group">
        <button shiro:hasPermission="sys:role:edit" class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>
        <button shiro:hasPermission="sys:role:remove" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
    </div>
    </script>
	<div th:include="include::footer"></div>
	<script>
		layui.use([ 'table' ], function() {
			var $ = layui.jquery;
			var table = layui.table;
			// 渲染表格
			var renderTable = function() {
				layer.load(2);
				table.render({
					elem : '#role-table',
					url : '/sys/role/list',
					toolbar : '#toolbarRole',
					page : true,
					parseData : chaoLayuiTable.parseData,
					request : chaoLayuiTable.request,
					response : chaoLayuiTable.response,
					//limit : 1,
					cols : [ [ {
						type : 'checkbox',
						fixed : 'left'
					}, {
						field : 'roleId',
						title : 'ID',
						fixed : 'left',
						unresize : true,
						sort : true,
						width : 80
					}, {
						field : 'roleName',
						align : 'center',
						title : '角色名',
					}, {
						field : 'remark',
						align : 'center',
						title : '备注',
					}, {
						title : '操作',
						toolbar : '#role-state',
						align : 'center'
					} ] ],
					done : function() {
						layer.closeAll('loading');
					}
				});

			};
			renderTable();
			//头工具栏事件
			table.on('toolbar(role-table)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'add'://增加
					openAdd();
					break;
				case 'batchRemove'://批量删除
					var data = checkStatus.data;
					if (data.length < 1) {
						layer.msg('请至少选择一个');
						break;
					}
					//批量删除
					batchRemove(data);
				}
			});

			//监听工具条
			table.on('tool(role-table)', function(obj) {
				var data = obj.data;
				var layEvent = obj.event;
				if (layEvent === 'del') {//删除
					remove(data.roleId);
				} else if (layEvent === 'edit') {//修改
					edit(data.roleId);
				}
			});

			function remove(id) {
				layer.confirm('确定要删除选中的记录？', {
					btn : [ '确定', '取消' ]
				}, function() {
					$.ajax({
						url : "/sys/role/remove",
						type : "post",
						data : {
							id : id
						},
						success : function(r) {
							if (r.retCode == '0000') {
								layer.msg("删除成功");
								renderTable();
							} else {
								layer.msg(r.retMsg);
							}
						}
					});
				})
			}

			function batchRemove(data) {
				layer.confirm("确认要删除选中的'" + data.length + "'条数据吗?", {
					btn : [ '确定', '取消' ]
				}, function() {
					var ids = new Array();
					$.each(data, function(i, row) {
						ids[i] = row['roleId'];
					});
					console.log(ids);
					$.ajax({
						type : 'POST',
						data : {
							"ids" : ids
						},
						url : '/sys/role/batchRemove',
						success : function(r) {
							if (r.retCode == '0000') {
								layer.msg(r.retMsg);
								renderTable();
							} else {
								layer.msg(r.retMsg);
							}
						}
					});
				});
			}

			function edit(id) {
				layer.open({
					type : 2,
					title : '角色修改',
					maxmin : true,
					shadeClose : false, // 点击遮罩关闭层
					area : [ '40%', '80%' ],
					content : '/sys/role/edit/' + id // iframe的url
				});
			}

			function openAdd() {
				layer.open({
					type : 2,
					title : '增加角色',
					maxmin : true,
					shadeClose : false, // 点击遮罩关闭层
					area : [ '40%', '60%' ],
					content : '/sys/role/add' // iframe的url
				});
			}

		});
	</script>
</body>
</html>